package grape.crm.service.customer.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import grape.common.service.trans.ITransService;
import grape.crm.service.customer.param.AddCustomerParam;
import grape.crm.service.customer.po.Customer;
import grape.common.service.common.IBaseService;

import java.util.Optional;

/**
 * <p>
 * 客户表 服务类
 * </p>
 *
 * @author yangwei
 * @since 2020-09-14
 */
public interface ICustomerService extends IBaseService<Customer>  , ITransService<String,String> {

    public static final String trans_customer_name = "trans_customer_name";
    public static final String trans_customer_code = "trans_customer_code";
    public static final String trans_customer_appellation = "trans_customer_appellation";
    public static final String trans_customer_name_else_appellation = "trans_customer_name_else_appellation";

    /**
     * 添加客户接口
     * @param addCustomerParam
     * @return
     */
    Customer addCustomer(AddCustomerParam addCustomerParam);

    /**
     * 根据编码查询
     * @param code
     * @return
     */
    default Customer getByCode(String code) {
        assertParamNotEmpty(code,"code 不能为空");
        Customer customer = new Customer();
        customer.setCode(code);
        return getOne(customer);
    }
    /**
     * 判断编码是否已存在
     * @param code
     * @return
     */
    default boolean existCode(String code){
        assertParamNotEmpty(code,"code 不能为空");
        Customer customer = new Customer();
        customer.setCode(code);
        int r = count(Wrappers.query(customer));
        return r > 0;
    }
    
    @Override
    default boolean support(String type){
        return isEqualAny(type,trans_customer_code, trans_customer_name, trans_customer_appellation,trans_customer_name_else_appellation);
    }

    @Override
    default String trans(String type, String key){
        Customer po = getById(key);
        if (po != null) {
            if (isEqual(type,trans_customer_name)) {
                return po.getName();
            }else if (isEqual(type,trans_customer_code)) {
                return po.getCode();
            }else if (isEqual(type,trans_customer_appellation)) {
                return po.getAppellation();
            }else if (isEqual(type,trans_customer_name_else_appellation)) {
                return Optional.ofNullable(po.getName()).orElse(po.getAppellation());
            }
        }
        return null;
    }
}
